package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.Station;
import com.jiawa.train.business.domain.StationExample;
import com.jiawa.train.business.mapper.StationMapper;
import com.jiawa.train.business.req.StationQueryReq;
import com.jiawa.train.business.req.StationSaveReq;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResult;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StationService {
    //reource是使用名称注入的方式而autowired是使用类型注入的方式
    @Resource
    private StationMapper stationMapper;

    //新增或者更新用户信息，通过判断是否有id决定进行新增操作还是更新操作
    public void save(StationSaveReq req) {
        //将传递的参数赋给Station对象,在req中没有id,createTime,updateTime的数据，需要自己进行赋值
        //这里的id使用雪花算法，然后创建时间使用当前时间
        DateTime now = DateTime.now();
        Station station = BeanUtil.copyProperties(req, Station.class);
        //如果id为空，则进行新增操作
        if(ObjectUtil.isNull(station.getId())){
            //保存之前先进行判断是不是已经存在该车站
            StationExample stationExample = new StationExample();
            stationExample.createCriteria().andNameEqualTo(station.getName());
            if(stationMapper.countByExample(stationExample) > 0){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_UNIQUE_ERROR);
            }
            station.setId(SnowUtil.getSnowflakeNextId());
            station.setCreateTime(now);
            station.setUpdateTime(now);
            stationMapper.insert(station);
        }
        //否则进行更新操作
        else{
            station.setUpdateTime(now);
            stationMapper.updateByPrimaryKeySelective( station);
        }
    }

    //如果用户id存在则进行根据id进行查询乘客的信息，如果是用户id不存在则进行查询所有,全部都按分页进行处理
    public PageResult queryStationList(StationQueryReq  req) {
        //创建stationExample对象
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("id desc");
        //设置查询条件
        StationExample.Criteria criteria = stationExample.createCriteria();

        // 先设置分页的参数
        PageHelper.startPage(req.getPage(),req.getPageSize());
        //进行查询数据库
        List<Station> stationList = stationMapper.selectByExample(stationExample);
        //返回查询结果,转为分页的形式,
        PageInfo<Station> pageInfo = new PageInfo<>(stationList);
        //获取数据进行封装
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        //返回查询结果
        return pageResult;
    }

    public void delete(Long id) {
        stationMapper.deleteByPrimaryKey(id);
    }

    public  List queryAll() {
        //创建stationExample对象
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("name_pinyin asc");
        List stationList = stationMapper.selectByExample(stationExample);
        return stationList;
    }
}